package com.pingan.haofang.searchcloud.common.validator;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * 参数校验处理器封装
 *
 * @author BAOJIE371
 * @date 2017/2/20
 * @since 1.0.0
 */
public final class ValidHandlerBean {

    private final Object bean;
    private final Method method;

    private ValidHandlerBean(Object bean, Method method) {
        this.bean = bean;
        this.method = method;
    }

    /**
     * 构造ValidHandlerBean
     *
     * @param bean
     * @param method
     * @return
     */
    public static ValidHandlerBean of(Object bean, Method method) {
        Objects.requireNonNull(bean, "bean is null");
        Objects.requireNonNull(method, "method is null");
        return new ValidHandlerBean(bean, method);
    }

    /**
     * 执行方法
     *
     * @param args
     * @return
     */
    public final Object invoke(Object... args) {
        try {
            return method.invoke(bean, args);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}

